/**
 * 
 */
package ru.olympus.antonyhands.ao.server.item;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import ru.olympus.antonyhands.ao.server.db.AOData;
import ru.olympus.antonyhands.ao.server.db.IPersistentClass;
import ru.olympus.antonyhands.ao.shared.util.Pair;
import ru.olympus.antonyhands.ao.shared.util.StringPair;
import ru.olympus.antonyhands.ao.shared.util.StringUtilities;


/**
 * @author AntonyHands
 * @since 14.02.2012
 */
public class ItemQuantityPair extends Pair<Item, Long> implements IPersistentClass
{

    public static final String TAG_NAME = "item-quantity-pair";

    private static final String ATTRIBUTE_KEY = "key";
    private static final String ATTRIBUTE_VALUE = "value";

    public ItemQuantityPair()
    {

    }

    public ItemQuantityPair(Item item, Long quantity)
    {
        super(item, quantity);
    }

    @Override
    public String getTagName()
    {
        return TAG_NAME;
    }

    @Override
    public List<StringPair> getAttributes()
    {
        List<StringPair> list = new ArrayList<StringPair>();
        list.add(new StringPair(ATTRIBUTE_KEY, getKey().getCode()));
        list.add(new StringPair(ATTRIBUTE_VALUE, getValue().toString()));
        return list;
    }

    @Override
    public void setAttributes(List<StringPair> attributes)
    {
        for (StringPair pair : attributes)
        {
            if (ATTRIBUTE_KEY.equals(pair.getKey()))
                setKey(AOData.getInstance().getItemByCode(pair.getValue()));
            else if (ATTRIBUTE_VALUE.equals(pair.getKey()))
                setValue(StringUtilities.stringToLong(pair.getValue()));
        }
    }

    @Override
    public Collection<IPersistentClass> getPersistentChildren()
    {
        return new ArrayList<IPersistentClass>();
    }

    @Override
    public void setPersistentChildren(List<IPersistentClass> persistentChildren)
    {
        // do nothing
    }
}